import modelEnhance from '@/utils/modelEnhance';
import PageHelper from '@/utils/pageHelper';

import { getMenuList } from "../service";

import { noticeGlobal } from "@/utils/core";

/**
 * 当第一次加载完页面时为true
 * 可以用这个值阻止切换页面时
 * 多次初始化数据
 */
let LOADED = false;
export default modelEnhance({
  namespace: 'menus',

  state: {
    pageData: PageHelper.create(),
    employees: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
      history.listen(({ pathname }) => {
        if (pathname === '/menus' && !LOADED) {
          LOADED = true;
          dispatch({
            type: 'init'
          });
        }
      });
    }
  },

  effects: {
    // 进入页面加载
    *init({ payload }, { call, put, select }) {
      const { pageData } = yield select(state => state.menus);
      yield put({
        type: 'getPageInfo',
        payload: {
          pageData: pageData.startPage(1, 10)
        }
      });
      // yield put({
      //   type: 'getEmployees'
      // });
    },
    // 获取分页数据
    *getPageInfo({ payload }, { call, put }) {
      const { pageData } = payload;
      // const {code, msg, data} = yield call(getRoleList, {pageNo: pageData.pageNum, pageSize: pageData.pageSize});
      const {code, msg, data} = yield call(getMenuList, pageData.pageinfoData());
      if (code == 200){
        yield put({
          type: 'setMenuList',
          payload: data
        })
      }else{
        noticeGlobal.error(msg);
      }
    },
    // 保存 之后查询分页
    *save({ payload }, { call, put, select, take }) {
      const { values, success } = payload;
      const { pageData } = yield select(state => state.menus);
      // put是非阻塞的 put.resolve是阻塞型的
      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: '/api/authManage/menu/addNewMenus',
          data: [values],
        }
      });

      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 修改
    *update({ payload }, { call, put, select }) {
      const { values, success } = payload;
      const {pageData} = yield select(state => state.menus);

      yield put.resolve({
        type: '@request',
        payload: {
          notice: true,
          url: '/api/authManage/menu/updateMenus',
          data: [values]
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },
    // 删除 之后查询分页
    *remove({ payload }, { call, put, select }) {
      const { records, success } = payload;
      const { pageData } = yield select(state => state.menus);
      yield put({
        type: '@request',
        payload: {
          method: 'DELETE',
          notice: true,
          url: '/api/authManage/menu/deleteMenus',
          data: {idList : records.map(item => item.id)}
        }
      });
      yield put({
        type: 'getPageInfo',
        payload: { pageData }
      });
      success();
    },

  },

  reducers: {
    setMenuList(state, {payload}){
      const { pageData } = state;
      return {
        ...state,
        pageData: pageData.setPage({data: {records : payload}}),
      }
    },
  }
});
